package com.br.aiops.storage;

import com.br.aiops.counter.Counter;
import com.br.aiops.storage.counter.LoadCounterService;
import com.br.aiops.storage.counter.StorageCounterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class PersistentCounterService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PersistentCounterService.class);

    @Autowired
    private Counter counter;

    @Autowired
    private StorageCounterService storageCounterService;

    @Autowired
    private LoadCounterService loadCounterService;

    public void storage() {
        //持久化，同时清除恢复统计数
        storageCounterService.storageCounter(counter.getCounter().values());
    }

    public void load(int partition) {
        loadCounterService.loadCounterByPartition(partition);
    }

    public void resume(String counterKey) {
        storageCounterService.resume(counterKey);
    }

    @Scheduled(cron = "0 */30 * * * ?")
    public void clearResumeData() {
        try {
            storageCounterService.deleteResume();
        } catch (Exception e) {
            LOGGER.error("清理统计数据（告警已恢复的数据）失败！", e);
        }
    }
}
